package com.coc.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
	// Action类公用私有变量，用来做页面导航标志
	private static String FORWARD = null;
	//用户名属性
	private String username;
	//密码属性
	private String password;
	//取得用户名值
	public String getUsername() {
		return username;
	}
	//设置用户名值
	public void setUsername(String username) {
		this.username = username;
	}
	//取得密码值
	public String getPassword() {
		return password;
	}
	//设置密码值
	public void setPassword(String password) {
		this.password = password;
	}

	public String execute() throws Exception {
		username = getUsername();  	//属性值即JSP页面上输入的值
		password = getPassword();   	//属性值即JSP页面上输入的值

		try {
			// 判断输入值是否是空对象或没有输入
			if (username != null && !username.equals("") && password != null
					&& !password.equals("")) {
				ActionContext.getContext().getSession().put("user", getUsername());
			// 根据标志内容导航到操作成功页面
				FORWARD = "success";
			} else {
			// 根据标志内容导航到操作失败页面
				FORWARD = "input";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return FORWARD;
	}
	
	public void validate() {
		if (getUsername() == null || getUsername().trim().equals("")) {
			//返回错误信息键值，user.required包含具体内容见messageResource.properties
			addFieldError("username", getText("user.required"));
		}
		if (getPassword() == null || getPassword().trim().equals("")) {
			//返回错误信息键值，pass.required包含具体内容见messageResource.properties
			addFieldError("password", getText("pass.required"));
		}
	}
}
